﻿using System;
using System.Collections.Generic;
using System.Text;

namespace WeighClient.Utils
{
    public static class ByteUtils
    {
        public static byte[] strToToHexByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);

            return returnBytes;
        }

        public static string byteToHexStr(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2");
                }
            }
            return returnStr;
        }

        public static int Asc(string character)
        {
            if (character.Length == 1)
            {
                System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
                int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];
                return (intAsciiCode);
            }
            else
            {
                throw new Exception("Character is not valid.");
            }

        }

        public static string Chr(int asciiCode)
        {
            if (asciiCode >= 0 && asciiCode <= 255)
            {
                System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
                byte[] byteArray = new byte[] { (byte)asciiCode };
                string strCharacter = asciiEncoding.GetString(byteArray);
                return (strCharacter);
            }
            else
            {
                throw new Exception("ASCII Code is not valid.");
            }
        }

        public static string[] MySplit(this string str, int count)
        {
            var list = new List<string>();
            int length = (int)Math.Ceiling((double)str.Length / count);

            for (int i = 0; i < length; i++)
            {
                int start = count * i;
                if (str.Length <= start)
                {
                    break;
                }
                if (str.Length < start + count)
                {
                    list.Add(str.Substring(start));
                }
                else
                {
                    list.Add(str.Substring(start, count));
                }
            }

            return list.ToArray();
        }

        public static string CalcXor(string hex)
        {
            int num1 = 0;
            int num2 = 0;

            num1 = Int32.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
            hex = hex.Remove(0, 2);

            while (hex.Length > 0)
            {
                num2 = Int32.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
                hex = hex.Remove(0, 2);
                num1 = num1 ^ num2;
            }
            num1 = ~num1;

            String hexStr = string.Format("{0:X000}", num1).PadLeft(2, '0');
            //Console.WriteLine(hexStr);
            return hexStr.Substring(6, 2);
        }
    }
}
